﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OpenCvSharpExtension
{
    using OpenCvSharp;
    using System.ComponentModel;

    /// <summary>
    /// 斑点检测器
    /// </summary>
    public class BlobDetector : System.ComponentModel.INotifyPropertyChanged
    {
        public BlobDetector()
        {
            createDetector();
        }
        private SimpleBlobDetector detector;

        public event PropertyChangedEventHandler PropertyChanged;

        private void createDetector()
        {
            //参数定义参见：http://brightguo.com/blob-detection-using-opencv-python-c/
            this.detector = SimpleBlobDetector.Create(new SimpleBlobDetector.Params()
            {
                //惯性率 0-1
                //正圆为1 线段为0  椭圆介于其间
                MaxInertiaRatio = MaxInertiaRatio,
                MinInertiaRatio = MinInertiaRatio,
                FilterByInertia = FilterByInertia,

                //圆度
                MaxCircularity = MaxCircularity,
                MinCircularity = MinCircularity,
                FilterByCircularity = FilterByCircularity,
                //凸度 0-1
                MinConvexity = MinConvexity,
                MaxConvexity = MaxConvexity,
                FilterByConvexity = FilterByConvexity,
                //面积
                MaxArea = MaxArea,
                MinArea = MinArea,
                FilterByArea = FilterByArea,

                //颜色
                BlobColor = BlobColor,
                FilterByColor = FilterByColor,//取值为0表示识别黑色斑点，为255识别白色斑点

                //斑点间的最小距离
                MinDistBetweenBlobs = MinDistBetweenBlobs,
                //斑点重复率
                MinRepeatability = MinRepeatability,

                //二值化阈值
                MaxThreshold = MaxThreshold,
                MinThreshold = MinThreshold,
                ThresholdStep = ThresholdStep,//二值化步长

            });


        }

        /// <summary>
        /// 检测图像中的斑点
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public KeyPoint[] Detect(Mat img)
        {
            var kps = this.detector.Detect(img);
            if (DrawKeyPoints)
            {
                Cv2.DrawKeypoints(img, kps, img);
            }
             

            return kps;
        }

        /// <summary>
        /// 是否绘制检测到的关键点
        /// </summary>
        public bool DrawKeyPoints { get; set; } = false;

        public float MaxInertiaRatio { get; set; }
        public float MinInertiaRatio { get; set; }
        public bool FilterByInertia { get; set; } = false;
        public float MaxCircularity { get; set; }
        public float MinCircularity { get; set; }
        public bool FilterByCircularity { get; set; } = false;
        public float MaxArea { get; set; }
        public float MinArea { get; set; }
        public bool FilterByArea { get; set; } = false;
        public byte BlobColor { get; set; }
        public bool FilterByColor { get; set; } = false;
        /// <summary>
        /// 最小斑点距离，用于合并过近的斑点
        /// </summary>
        public float MinDistBetweenBlobs { get; set; }
        public uint MinRepeatability { get; set; } = 0;
        public float MaxThreshold { get; set; } = 255;
        public float MinThreshold { get; set; } = 0;
        public float ThresholdStep { get; set; } = 2;
        public float MinConvexity { get; set; } = 0;
        public float MaxConvexity { get; set; } = 1;
        public bool FilterByConvexity { get; set; } = false;
    }
}
